import plugin from'../../../lib/plugins/plugin.js'
import fs from 'fs'
import YAML from 'yaml'

class Yaml {
  constructor () {}
  
//读
getread(path) {
 try {
    var file = fs.readFileSync(path, 'utf8');
  } catch (e) {
    console.log(e);
    return false;
  }
  //转换
  return YAML.parse(file);
}

//✍🏻
getwrite(path,data) {
  try {
    //转换
    let 数据 = YAML.stringify(data);
    fs.writeFileSync(path, 数据, 'utf8');
    return true
  } catch (e) {
    //错误处理
    console.log(e);
    return false
  }
}





















/****
**为了以后写代码方便点,以下写法会保留yaml的注释(自己胡乱写的)
** keyname为key名
****/

//读👀
get(path){
let 读取
try{
读取 = fs.readFileSync(path, 'utf8');
}catch(err){
logger.error(err)
return false
}
return YAML.parse(读取)
}


//写✍🏻
set(path,keyname,keys){
let 读
try{
读 = fs.readFileSync(path, 'utf8');
}catch(err){
logger.error(err)
return false
}
let D=YAML.parseDocument(读)
try {
D.setIn(keyname.split('.'),keys)
fs.writeFileSync(path,YAML.stringify(D),"utf8");
}catch(err){
logger.error(err)
return false
}
}

//添加值
add(path,keyname,value) {
let du
try{
du = fs.readFileSync(path, 'utf8');
}catch(err){
logger.error(err)
return false
}
let DD=YAML.parseDocument(du)
try {
DD.addIn(keyname.split('.'),value)
fs.writeFileSync(path,YAML.stringify(DD),"utf8");
}catch(err){
logger.error(err)
return false
}
}

//删除值(需要多传入一个keys,value只是keys中的一个成员)
del(path,keyname,keys,value){
if(keys.indexOf(value)!== -1){
keys.splice(keys.indexOf(value), 1)
logger.info('del成功')
}
this.set(path,keyname,keys)
}




}
  
  
export default new Yaml()